package com.zyndev.sanchi.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

    public static List<String> matches(String str, String regex) {
        List<String> empty = new ArrayList<>();
        if (str == null) {
            return empty;
        }
        return matcheAll(regex, str, empty);
    }

    /**
     * Matche all e.
     *
     * @param <E>        the type parameter
     * @param regex      the regex
     * @param str        the str
     * @param collection the collection
     * @return the e
     */
    public static <E> E matcheAll(String regex, String str, Collection<String> collection) {
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        String val;
        while (m.find()) {
            val = m.group();
            collection.add(val);
        }
        return (E) collection;
    }
}
